class TempExaminationResoultsController < ApplicationController

  def confirm
    @temp_examination_resoult = TempExaminationResoult.find(params[:id])
    @examination_resoult = ExaminationResoult.create(
      :order_examination_id => @temp_examination_resoult.order_examination_id,
      :description =>  @temp_examination_resoult.description,
      :user_id => current_user.id,
      :executing_user_id => @temp_examination_resoult.user_id
    )
    for p in @temp_examination_resoult.param_resoults
      ParamResoult.create(
        :examination_resoult_id => @examination_resoult.id,
        :param_norm_id => p.param_norm_id,
        :description => p.description,
        :resoult => p.resoult
      )
    end
    OrderExamination.update(
      @examination_resoult.order_examination_id,
      :is_checked => true,
      :checking_user_id => current_user.id
    )
    TempExaminationResoult.delete(params[:id])
  end

  def decline
     @temp_examination_resoult = TempExaminationResoult.find(params[:id])
    OrderExamination.update(@temp_examination_resoult.order_examination_id, :has_resoult => false)
  end

  def show
    @temp_examination_resoult = TempExaminationResoult.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @temp_examination_resoult }
    end
  end

  def destroy
    @temp_examination_resoult = TempExaminationResoult.find(params[:id])
    @temp_examination_resoult.destroy

    respond_to do |format|
      format.html { redirect_to(examination_resoults_url) }
      format.xml  { head :ok }
    end
  end


  def asdf
#    params[:temp_examination_resoult][param_resoult_attributes][]
  end

  def edit
    @temp_exam_resoult = TempExaminationResoult.find params :id
  end

   # POST /exam_orders
  # POST /exam_orders.xml
  def create
    @temp_examination_resoult = TempExaminationResoult.new(params[:temp_examination_resoult])

    respond_to do |format|
      if @temp_examination_resoult.save
        format.html { redirect_to :controller=>"order_examinations_for_technik" }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @temp_examination_resoult.errors, :status => :unprocessable_entity }
      end
    end
  end


  #  # PUT /temp_examination_resoults/1
  #  # PUT /temp_examination_resoults/1.xml
  def update
    @temp_examination_resoult = TempExaminationResoult.find(params[:id])
    respond_to do |format|
      if @temp_examination_resoult.update_attributes(params[:temp_examination_resoult])
        format.html { redirect_to :controller=>"order_examinations_for_technik" }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @temp_examination_resoult.errors, :status => :unprocessable_entity }
      end
      if(params[:confirm_button])
        confirm
      else if(params[:decline_button])
          decline
        end
      end
    end
  end
end
